<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport" >导入全表Excel</Button>
        </form>
        <input v-model="id" placeholder="id" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出全表EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns"></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑activeevent"
        v-model="editActiveEventModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="100">
        <Form-item label="id">
            <Input v-model="formEdit.id" disabled ></Input>
        </Form-item>
        <Form-item label="name">
            <Input v-model="formEdit.name" ></Input>
        </Form-item>
        <Form-item label="desc">
            <Input v-model="formEdit.desc"></Input>
        </Form-item>
        <Form-item label="type">
            <Input v-model="formEdit.type"></Input>
        </Form-item>
        <Form-item label="startType">
            <Input v-model="formEdit.startType"></Input>
        </Form-item>
        <Form-item label="startTime">
            <Input v-model="formEdit.startTime"></Input>
        </Form-item>
        <Form-item label="endTime">
            <Input v-model="formEdit.endTime"></Input>
        </Form-item>
        <Form-item label="openLevel">
            <Input v-model="formEdit.openLevel"></Input>
        </Form-item>
        <Form-item label="closeTime">
            <Input v-model="formEdit.closeTime"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getActiveEvents, getAllActiveEvents, modifyActiveEvent, importActiveEvents} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      id: '',
      filename: 'active_event',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      editActiveEventModal: false,
      results: '',
      formEdit: {
        id: '',
        name: '',
        desc: '',
        type: '',
        startType: '',
        startTime: '',
        endTime: '',
        openLevel: '',
        closeTime: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tId = 0
      if (this.id !== '') {
        tId = parseInt(this.id)
      }

      console.log('this.tId:', tId)
      getActiveEvents(tId, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let ActiveEventVO = getVO('ActiveEventVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = ActiveEventVO.decode(pv.data[i])
          vo.id = vo.id.toNumber()
          vo.startTime = vo.startTime.toNumber()
          vo.endTime = vo.endTime.toNumber()
          vo.closeTime = vo.closeTime.toNumber()
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditActiveEvent (index) {
      let data = this.tableData[index]
      this.formEdit.id = data.id
      this.formEdit.name = data.name
      this.formEdit.desc = data.desc
      this.formEdit.type = data.type
      this.formEdit.startType = data.startType
      this.formEdit.startTime = data.startTime
      this.formEdit.endTime = data.endTime
      this.formEdit.openLevel = data.openLevel
      this.formEdit.closeTime = data.closeTime

      this.editActiveEventModal = true
    },
    onSubmit () {
      if (this.formEdit.id === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let ActiveEventVO = getVO('ActiveEventVO')
      var activeEventVO = new ActiveEventVO()
      activeEventVO.id = Number(this.formEdit.id)
      activeEventVO.name = this.formEdit.name
      activeEventVO.desc = this.formEdit.desc
      activeEventVO.type = parseInt(this.formEdit.type)
      activeEventVO.startType = parseInt(this.formEdit.startType)
      activeEventVO.startTime = Number(this.formEdit.startTime)
      activeEventVO.endTime = Number(this.formEdit.endTime)
      activeEventVO.openLevel = parseInt(this.formEdit.openLevel)
      activeEventVO.closeTime = Number(this.formEdit.closeTime)

      modifyActiveEvent(activeEventVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.id === this.formEdit.id) {
              this.tableData[i].name = this.formEdit.name
              this.tableData[i].desc = this.formEdit.desc
              this.tableData[i].type = this.formEdit.type
              this.tableData[i].startType = this.formEdit.startType
              this.tableData[i].startTime = this.formEdit.startTime
              this.tableData[i].endTime = this.formEdit.endTime
              this.tableData[i].openLevel = this.formEdit.openLevel
              this.tableData[i].closeTime = this.formEdit.closeTime
              break
            }
          }
        }
      })
    },
    onImport () {
      // 导入EXCEL
      importActiveEvents(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllActiveEvents().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let ActiveEventVO = getVO('ActiveEventVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = ActiveEventVO.decode(pv.data[i])
          // vo.id = vo.id.toNumber()
          // vo.startTime = vo.startTime.toNumber()
          // vo.endTime = vo.endTime.toNumber()
          // vo.closeTime = vo.closeTime.toNumber()
          // vo.startTime = "<td style=\"mso-number-format:'\\@';\">" + vo.startTime.toNumber() + '</td>'
          // vo.endTime = "<td style=\"mso-number-format:'\\@';\">" + vo.endTime.toNumber() + '</td>'
          // vo.closeTime = "<td style=\"mso-number-format:'\\@';\">" + vo.closeTime.toNumber() + '</td>'
          vo.id = '' + vo.id.toNumber()
          vo.startTime = '' + vo.startTime.toNumber()
          vo.endTime = '' + vo.endTime.toNumber()
          vo.closeTime = '' + vo.closeTime.toNumber()
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'id',
          key: 'id',
          align: 'center'
        },
        {
          title: 'name',
          key: 'name',
          align: 'center'
        },
        {
          title: 'desc',
          key: 'desc',
          align: 'center'
        },
        {
          title: 'type',
          key: 'type',
          align: 'center'
        },
        {
          title: 'startType',
          key: 'startType',
          align: 'center'
        },
        {
          title: 'startTime',
          key: 'startTime',
          align: 'center'
        },
        {
          title: 'endTime',
          key: 'endTime',
          align: 'center'
        },
        {
          title: 'openLevel',
          key: 'openLevel',
          align: 'center'
        },
        {
          title: 'closeTime',
          key: 'closeTime',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditActiveEvent(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
